/// @tags: SAM
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define rint register int

using std::cin;
using std::cout;
using std::max;

namespace BlueQuantum {

constexpr int maxn = 1e5 + 3, samn = maxn * 2, charset = 26;

int tot = 1, lst = 1;
int ch[samn][charset], len[samn], link[samn];

inline void extend(int c) {
  int np = ++tot, p = lst;
  lst = tot;
  len[np] = len[p] + 1;
  while (p && ch[p][c] == 0) ch[p][c] = np, p = link[p];
  if (p == 0) {
    link[np] = 1;
    return;
  }
  int q = ch[p][c];
  if (len[q] == len[p] + 1) {
    link[np] = q;
    return;
  }
  int nq = ++tot;
  memcpy(ch[nq], ch[q], sizeof(ch[nq]));
  len[nq] = len[p] + 1;
  link[nq] = link[q];
  link[q] = link[np] = nq;
  while (p && ch[p][c] == q) ch[p][c] = nq, p = link[p];
}

char str[maxn];
int f[samn], deg[samn], siz[samn];
std::vector<int> e[samn];

inline void init() {
  memset(f, 0, sizeof(f));
  memset(deg, 0, sizeof(deg));
  memset(siz, 0, sizeof(siz));
  memset(link, 0, sizeof(link));
  memset(len, 0, sizeof(len));
  memset(ch, 0, sizeof(ch));
  tot = lst = 1;
}

void dfs(int x) {
  for (auto v : e[x]) dfs(v), siz[x] += siz[v];
}

inline int main() {
  int T;
  cin >> T;
  while (T--) {
    int n;
    cin >> n;
    init();
    for (int i = 1; i <= n; ++i) {
      cin >> (str + 1);
      int m = strlen(str + 1);
      for (int j = 1; j <= m; ++j) extend(str[j] - 'a'), siz[lst]++;
      lst = 1;
    }
    for (int i = 0; i <= tot; ++i) e[i].clear();
    for (int i = 2; i <= tot; ++i) e[link[i]].push_back(i), deg[i]++;
    dfs(1);
    for (int i = 1; i <= tot; ++i)
      for (int j = 0; j < charset; ++j)
        if (ch[i][j]) deg[ch[i][j]]++;
    std::queue<int> q;
    q.push(1);
    int ans = 0;
    while (q.size()) {
      int u = q.front();
      q.pop();
      ans = max(ans, f[u]);
      for (auto v : e[u]) {
        f[v] = max(f[v], f[u] + (len[v] - len[u]) * siz[v]);
        deg[v]--;
        if (!deg[v]) q.push(v);
      }
      for (int i = 0; i < charset; ++i)
        if (ch[u][i]) {
          int v = ch[u][i];
          f[v] = max(f[v], f[u] + siz[v]);
          deg[v]--;
          if (!deg[v]) q.push(v);
        }
    }
    cout << ans << '\n';
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("fake.in", "r", stdin);
  freopen("fake.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
